rm(list = ls())

# these are the tables in the paper, to be ran after the estimates are ready

library(checkpoint)
library(texreg)

checkpoint("2018-06-15", project = "own_wd_path", checkpointLocation = "cp_location")

wd <- "own_wd"
type <- ".html"

resultspath <- "results_path" #from models_in_paper.R

load(file=paste0(resultspath, 'm_1.RData'))
load(file=paste0(resultspath, 'm_2.RData'))
load(file=paste0(resultspath, 'm_3.RData'))
load(file=paste0(resultspath, 'm_4.RData'))
load(file=paste0(resultspath, 'm_5.RData'))
load(file=paste0(resultspath, 'm_6.RData'))


###########
# Table 1 #
###########

htmlreg(l=list(m.1[[1]], m.1[[2]], m.2[[1]]),
        digits=2, file=paste0(wd, "table1", type),
        caption="",
        single.row=TRUE,
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               region2="Southern Europe (base: Northwestern)",
                               region3="Eastern Europe",
                               year_num="Year of Survey",
                               "region2:rideol1"="Ext Left*SE",
                               "region2:rideol2"="Left*SE",
                               "region2:rideol3"="Center*SE",
                               "region2:rideol4"="Right*SE",
                               "region2:rideol5"="Ext Right*SE",
                               "region3:rideol1"="Ext Left*EE",
                               "region3:rideol2"="Left*EE",
                               "region3:rideol3"="Center*EE",
                               "region3:rideol4"="Right*EE",
                               "region3:rideol5"="Ext Right*EE",
                               "rideol1:year_num"="Ext Left*Year",
                               "rideol3:year_num"="Center*Year",
                               "rideol4:year_num"="Right*Year",
                               "rideol5:year_num"="Ext Right*Year",
                               "region2:rideol1:year_num"="SE*Year*Ext Left",
                               "region3:rideol1:year_num"="EE*Year*Ext Left",
                               "region2:rideol3:year_num"="SE*Year*Center",
                               "region3:rideol3:year_num"="EE*Year*Center",
                               "region2:rideol4:year_num"="SE*Year*Right",
                               "region3:rideol4:year_num"="EE*Year*Right",
                               "region2:rideol5:year_num"="SE*Year*Ext Right",
                               "region3:rideol5:year_num"="EE*Year*Ext Right"))


###############################
# Table 2 - regression models #
###############################

htmlreg(l=list(m.2[[2]], m.3[[1]], m.3[[2]]),
        digits=2, file=paste0(wd, "table2", type),
        single.row=TRUE,
        caption="",
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               soc_pop_eleches="Exposure",
                               region3="Eastern Europe (base: Southern Europe)",
                               "soc_pop_eleches:region3"="Exposure*EE",
                               "region3:soc_pop_eleches"="Exposure*EE",
                               "region3:rideol1"="Ext Left*EE",
                               "region3:rideol2"="Left*EE",
                               "region3:rideol3"="Center*EE",
                               "region3:rideol4"="Right*EE",
                               "region3:rideol5"="Ext Right*EE",
                               "rideol1:soc_pop_eleches"="Ext Left*Exposure",
                               "rideol2:soc_pop_eleches"="Left*Exposure",
                               "rideol3:soc_pop_eleches"="Center*Exposure",
                               "rideol4:soc_pop_eleches"="Right*Exposure",
                               "rideol5:soc_pop_eleches"="Ext Right*Exposure",
                               "region3:rideol1:soc_pop_eleches"="EE*Ext Left*Exposure",
                               "region3:rideol2:soc_pop_eleches"="EE*Left*Exposure",
                               "region3:rideol3:soc_pop_eleches"="EE*Center*Exposure",
                               "region3:rideol4:soc_pop_eleches"="EE*Right*Exposure",
                               "region3:rideol5:soc_pop_eleches"="EE*Ext Right*Exposure"))

#################
#### Table 3 ####
#################

htmlreg(l=list(m.4[[1]], m.4[[2]]),
        caption = "",  
        digits=2, file=paste0(wd, "table3", type),
        single.row=TRUE,
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               region2="Southern Europe (base: Northwestern)",
                               region3="Eastern Europe",
                               "region2:prtdgcl_num"="SE:Party ID",
                               "region3:prtdgcl_num"="EE:Party ID",
                               "region2:rideol1"="SE:Ext Left",
                               "region3:rideol1"="EE:Ext Left",
                               "region2:rideol3"="SE:Center",
                               "region3:rideol3"="EE:Center",
                               "region2:rideol4"="SE:Right",
                               "region3:rideol4"="EE:Right",
                               "region2:rideol5"="SE:Ext Right",
                               "region3:rideol5"="EE:Ext Right",
                               "rideol1:prtdgcl_num"="Ext Left:Party ID",
                               "rideol3:prtdgcl_num"="Center:Party ID",
                               "rideol4:prtdgcl_num"="Right:Party ID",
                               "rideol5:prtdgcl_num"="Ext Right:Party ID",
                               "region2:rideol1:prtdgcl_num"="SE:Ext Left:Party ID",
                               "region3:rideol1:prtdgcl_num"="EE:Ext Left:Party ID",
                               "region2:rideol3:prtdgcl_num"="SE:Center:Party ID",
                               "region3:rideol3:prtdgcl_num"="EE:Center:Party ID",
                               "region2:rideol4:prtdgcl_num"="SE:Right:Party ID",
                               "region3:rideol4:prtdgcl_num"="EE:Right:Party ID",
                               "region2:rideol5:prtdgcl_num"="SE:Ext Right:Party ID",
                               "region3:rideol5:prtdgcl_num"="EE:Ext Right:Party ID"))


#################
#### Table 4 ####
#################

htmlreg(l=list(m.5[[1]], m.5[[2]], m.6[[1]]),
        caption = "",  
        digits=2, file=paste0(wd, "table4", type),
        single.row=TRUE,
        custom.gof.names = c(NA,
                             NA,
                             NA,
                             NA,
                             "Country*Years",
                             "Countries",
                             "Var Country*Years: Intercept",
                             "Var Country*Years: Extreme Left",
                             "Var Country*Years: Center",
                             "Var Country*Years: Right",
                             "Var Country*Years: Extreme Right",
                             "Cov Country*Years: Intercept, Ext Left",
                             "Cov Country*Years: Intercept, Center",
                             "Cov Country*Years: Intercept, Right",
                             "Cov Country*Years: Intercept, Ext Right",
                             "Cov Country*Years: Ext Left, Center",
                             "Cov Country*Years: Ext Left, Right",
                             "Cov Country*Years: Ext Left, Ext Right",
                             "Cov Country*Years: Center, Right",
                             "Cov Country*Years: Center, Ext Right",
                             "Cov Country*Years: Right, Ext Right",
                             "Var Country: Intercept"),
        custom.coef.map = list("(Intercept)"="Intercept",
                               rideol1="Extreme left (base: left)",
                               rideol3="Center",
                               rideol4="Right",
                               rideol5="Extreme right",
                               gov_dist="Distance from the gov.",
                               union1 = "Union member (dichot.)",
                               prtdgcl_num = "Party ID (cont.)",
                               gender1="Male",
                               agea="Age (cont.)",
                               eduyrs="Years of education (cont.)",
                               domicil2="Suburbs (base: big city)",
                               domicil3="Small City",
                               domicil4="Village",
                               domicil5="Countryside",
                               unemployed1="Unemployed",
                               gov_genlr="Gov. L-R",
                               gdp_wb_ppp="GDP (ppp, 2011 US$)",
                               wb_vae="Voice & Accountability",
                               region2="Southern Europe (base: Northwestern)",
                               region3="Eastern Europe",
                               "region2:gov_dist"="Gov. dist*SE",
                               "region3:gov_dist"="Gov. dist*EE",
                               "region2:gov_genlr"="Gov. L-R*SE",
                               "region3:gov_genlr"="Gov. L-R*EE",
                               "gov_genlr:gov_dist"="Gov. L-R*Gov. dist",
                               "region2:gov_genlr:gov_dist"="Gov. L-R*SE*Gov. dist",
                               "region3:gov_genlr:gov_dist"="Gov. L-R*EE*Gov. dist"))
